<template>
  <view class="uni-container">
    <uni-forms ref="form" :value="formData" validateTrigger="bind">
      <uni-forms-item name="avatorfile" label="成员头像" required>
        <uni-file-picker file-mediatype="image" return-type="object" v-model="formData.avatorfile"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="open_url" label="点击目标地址">
        <uni-easyinput placeholder="点击跳转目标地址。如果是web地址则使用内置web-view打开；如果是本地页面则跳转本地页面；如果是schema地址则打开本地的app" v-model="formData.open_url" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="name" label="成员姓名" required>
        <uni-easyinput placeholder="成员姓名" v-model="formData.name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="title" label="职称" required>
        <uni-data-checkbox v-model="formData.title" :localdata="formOptions.title_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="researchDirection" label="研究方向" required>
        <uni-easyinput placeholder="成员的研究方向 如图像处理" v-model="formData.researchDirection" trim="both"></uni-easyinput>
      </uni-forms-item>
	  <uni-forms-item name="content" label="个人简历">
	  	<!-- <uni-easyinput placeholder="个人简历" v-model="formData.content" /> -->
	  	<Tinymce ref="editor" v-model="formData.content" :width="860" :height="400" />
	  </uni-forms-item>
      <uni-forms-item name="category_id" label="分类id">
        <uni-easyinput placeholder="多个栏目的banner都存在一个表里时可用这个字段区分" v-model="formData.category_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="status" label="生效状态">
        <switch @change="binddata('status', $event.detail.value)" :checked="formData.status"></switch>
      </uni-forms-item>
      <uni-forms-item name="description" label="备注">
        <uni-easyinput placeholder="维护者自用描述" v-model="formData.description" trim="both"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/lab-member.js';
  import Tinymce from '@/components/Tinymce'
  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'lab-member';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  export default {
	components: {
	  	Tinymce,
	  },
    data() {
      let formData = {
        "avatorfile": null,
        "open_url": "",
        "name": "",
        "title": null,
        "researchDirection": "",
        "content": "",
        "category_id": "",
        "status": true,
        "description": ""
      }
      return {
        formData,
        formOptions: {
          "title_localdata": [
            {
              "value": 0,
              "text": "教授"
            },
            {
              "value": 1,
              "text": "副教授"
            },
            {
              "value": 2,
              "text": "讲师"
            },
            {
              "value": 3,
              "text": "博士研究生"
            },
            {
              "value": 4,
              "text": "硕士研究生"
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("avatorfile,open_url,name,title,researchDirection,content,category_id,status,description").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
